package com.design.patterns.list.command;

import com.design.patterns.list.command.light.*;
import com.design.patterns.list.command.tv.TvOffCommand;
import com.design.patterns.list.command.tv.TvOnCommand;
import com.design.patterns.list.command.tv.TvReceiver;

/**
 * 客户端
 * @author Wangzhiwen
 */
public class CommandClient {

    public static void main(String[] args) {
        // 命令接收者
        Receiver receiver = new Receiver();

        // 具体的命令，聚合命令接收者。与命令发布者交互
        Command command = new ConcreteCommand(receiver);

        // 命令发布者
        Invoker invoker = new Invoker();

        // 设置单个命令，可以直接执行和撤销
        invoker.setCommands(command);
        invoker.executeCommand();
        invoker.undoCommand();

        // 清空命令
        invoker.cleanCommands();

        // 命令接收者组
        LightReceiver incandescentLightReceiver = new IncandescentLightReceiver();
        LightReceiver spotLightReceiver = new SpotLightReceiver();

        Command incandescentLightOnCommand = new LightOnCommand(incandescentLightReceiver);
        Command spotLightOnCommand = new LightOnCommand(spotLightReceiver);

        Command incandescentLightOffCommand = new LightOffCommand(incandescentLightReceiver);
        Command spotLightOffCommand = new LightOffCommand(spotLightReceiver);

        // 设置多个命令
        invoker.setCommands(incandescentLightOnCommand,spotLightOnCommand,incandescentLightOffCommand,spotLightOffCommand);

        TvReceiver tvReceiver = new TvReceiver();

        Command tvOnCommand = new TvOnCommand(tvReceiver);
        Command tvOffCommand = new TvOffCommand(tvReceiver);

        invoker.setCommands(tvOnCommand,tvOffCommand);

        invoker.executeCommands();
        invoker.undoCommands();

        // 根据唯一的命令标识符执行该命令
        invoker.executeCommand(incandescentLightOffCommand.commandIdentification());
        invoker.undoCommand(tvOnCommand.commandIdentification());
    }

}
